package com.example.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.resource.MoneyIn;
import com.example.resource.MoneyOut;
import com.yijiu.trb.R;

public class MoneyInOutAdapter extends MyBaseAdapter
{
	private Context context;
	private View progressBar;
	private SimpleDateFormat sdf = new SimpleDateFormat("MM-dd日");
	private boolean isOut;
	
	public MoneyInOutAdapter(Context context, boolean isOut, List moneyList)
	{
		this.context = context;
		this.isOut = isOut;
		this.list = moneyList;
	}
	@Override
	public int getCount()
	{
		if (isShowProgress)
		{
			return list.size() + 1;
		}
		return list.size();
	}
	@Override
	public Object getItem(int position)
	{
		if (isShowProgress)
		{
			return null;
		}
		return list.get(position);
	}
	@Override
	public long getItemId(int position)
	{
		return position;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		if (position == list.size())
		{
			if (progressBar == null)
			{
				progressBar = LayoutInflater.from(context).inflate(R.layout.load_next_page_layout, null);
			}
			return progressBar;
		}
		else
		{
			convertView = LayoutInflater.from(context).inflate(R.layout.money_out_adapter_layout, null);
			TextView monthTextView = (TextView) convertView.findViewById(R.id.tv_money_out_adapter_month);
			TextView startEndTextView = (TextView) convertView.findViewById(R.id.tv_money_out_adapter_month_start_end);
			// TextView allTextView = (TextView)
			// convertView.findViewById(R.id.tv_money_out_adapter_all);
			TextView nameTextView = (TextView) convertView.findViewById(R.id.tv_money_out_adapter_name);
			TextView moneyTextView = (TextView) convertView.findViewById(R.id.tv_money_out_adapter_money);
			TextView dateTextView = (TextView) convertView.findViewById(R.id.tv_money_out_adapter_date);
			TextView statusTextView = (TextView) convertView.findViewById(R.id.tv_money_out_adapter_status);
			TextView waterTextView = (TextView) convertView.findViewById(R.id.tv_money_out_adapter_water);
			LinearLayout llayoutAllIn = (LinearLayout) convertView.findViewById(R.id.llayout_money_out_adapter_all_in);
			LinearLayout llayoutIsShow = (LinearLayout) convertView.findViewById(R.id.llayout_money_in_out_is_show);
			TextView shouxuTextView = (TextView) convertView.findViewById(R.id.tv_money_out_adapter_shouxu);
			if (!isOut)
			{
				MoneyIn moneyIn = (MoneyIn) list.get(position);
				llayoutAllIn.setVisibility(View.VISIBLE);
				shouxuTextView.setVisibility(View.GONE);
				nameTextView.setText(moneyIn.bankName);
				Date moneyDate = new Date(moneyIn.payTime);
				monthTextView.setText(String.valueOf(moneyDate.getMonth() + 1));
				moneyTextView.setText("￥" + moneyIn.amount);
				dateTextView.setText(sdf.format(moneyDate));
				statusTextView.setText(moneyIn.status);
				int count = daysCount(moneyDate.getYear(), moneyDate.getMonth());
				startEndTextView.setText((moneyDate.getMonth() + 1) + ".01-" + (moneyDate.getMonth() + 1) + "." + count);
				waterTextView.setText("流水号：" + moneyIn.outBizNo);
				// 显示index
				String currentStr = String.valueOf(moneyDate.getYear() + "-" + (moneyDate.getMonth() + 1));
				// 上一项的index
				String previewStr = (position - 1) >= 0 ? String.valueOf(new Date(((MoneyIn) list.get(position - 1)).payTime)
						.getYear() + "-" + (new Date(((MoneyIn) list.get(position - 1)).payTime).getMonth() + 1)) : " ";
				/**
				 * 判断是否上一次的存在
				 */
				if (!previewStr.equals(currentStr))
				{
					llayoutIsShow.setVisibility(View.VISIBLE);
				}
				else
				{
					llayoutIsShow.setVisibility(View.GONE);
				}
			}
			else
			{
				MoneyOut moneyOut = (MoneyOut) list.get(position);
				llayoutAllIn.setVisibility(View.GONE);
				shouxuTextView.setVisibility(View.VISIBLE);
				nameTextView.setText(moneyOut.bankName);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date moneyDate = null;
				try
				{
					moneyDate = sdf.parse(moneyOut.payTime);
				}
				catch (ParseException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				monthTextView.setText(String.valueOf(moneyDate.getMonth() + 1));
				moneyTextView.setText("￥" + moneyOut.amountIn);
				dateTextView.setText(moneyOut.payTime);
				statusTextView.setText(moneyOut.status);
				int count = daysCount(moneyDate.getYear(), moneyDate.getMonth());
				startEndTextView.setText((moneyDate.getMonth() + 1) + ".01-" + (moneyDate.getMonth() + 1) + "." + count);
				waterTextView.setText("流水号：" + moneyOut.outBizNo);
				shouxuTextView.setText("手续费：" + moneyOut.charge + "元");
				// 显示index
				String currentStr = moneyOut.payTime.substring(0, 7);
				// 上一项的index
				String previewStr = (position - 1) >= 0 ? ((MoneyOut) list.get(position - 1)).payTime.substring(0, 7) : " ";
				/**
				 * 判断是否上一次的存在
				 */
				if (!previewStr.equals(currentStr))
				{
					llayoutIsShow.setVisibility(View.VISIBLE);
				}
				else
				{
					llayoutIsShow.setVisibility(View.GONE);
				}
			}
			if (position >= list.size() - 1 && listener != null)
			{
				listener.onFoot();
			}
			return convertView;
		}
	}
	private int daysCount(int year, int month)
	{
		Calendar time = Calendar.getInstance();// 使用默认时区和语言环境获得一个日历
		time.clear();
		time.set(Calendar.YEAR, year);
		time.set(Calendar.MONTH, month - 1);// 注意Calendar对象默认一月是为零的
		return time.getActualMaximum(Calendar.DAY_OF_MONTH);// 获得本月份的天数
	}
}
